package srzy.module

import java.io.File
import srzy.common.Common
import srzy.common.Config
import scala.util.control.Breaks._
import scala.collection.immutable.Nil
object CleanDir extends AbsCaseDeal with srzy.common.Logging {

  def deal = {
    logger.debug("start clean dir")
    //    while (true) {
    val id = new File(inputDir)
    val ncd = normalCdrDir.map(new File(_))
    val ecd = errCdrDir.map(new File(_))
    val tcd = transCdrDir.map(new File(_))
    //    val l = cd.listFiles().toList
    val l = id.listFiles().toList ::: ncd.flatMap(_.listFiles()) ::: ecd.flatMap(_.listFiles()) ::: tcd.flatMap(_.listFiles())
    val l2 = l.filter(x => !(x.getName() matches """[a-zA-Z0-9_.]*\.(dcfmt|r|err|cdr|tmp|trans|transerr)"""))
    if (l2.length != 0) {
      logger.error("illegal file name:" + l2.map(_.getName()).mkString(" "))
      throw new Exception("文件后缀错误!")
    }
    var flag = true
    for (i <- l) {
      logger.debug("del:" + i.getName())
      val dr = i.delete()
      logger.debug("delRes:" + dr)
      if (dr == false) flag = false
    }
    Thread.sleep(500)

    logger.debug("inputDir:" + new File(inputDir).listFiles().toList.mkString(" "))
    logger.debug("normalCdrDir:" + normalCdrDir.map(new File(_)).flatMap(_.listFiles()).mkString(" "))
    logger.debug("errCdrDir:" + errCdrDir.map(new File(_)).flatMap(_.listFiles()).mkString(" "))
    logger.debug("transCdrDir:" + transCdrDir.map(new File(_)).flatMap(_.listFiles()).mkString(" "))
    if (flag == false) {
      deal
    }

  }
}
